<?php

  /*

      TravianWAP
      Copyright (c) 2007-08 by Ananas - All rights reserved
      http://n00bz.biz/travianwap/
      Contact: uroskn@gmail.com

      --------------------------------------------------------------------------

      This program is free software: you can redistribute it and/or modify
      it under the terms of the GNU  General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.

      This program is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.

      You should have received a copy of the GNU  General Public License
      along with this program. If not, see http://www.gnu.org/licenses/.

  */

  define("MODULE_OK", 1);
  if (!defined("MAIN_UNIT")) die("h4xx0r");
  global $txt, $req;
  
  printtop($txt);
  
  if (empty($req["ret"]))
  {
    $return = "village_look";
    $file = "dorf2";
  }
  else
  {
    $return = $req["ret"];
    $file = "dorf1";
  }
  
  $info = travian_return_construct_info($_SESSION, $req["id"], False);
  $first = "";
  if (count($info) > 1)
  {
    echo "<strong>Construct new building</strong><br/>";
    $first = "<br/>";
  }
  for ($i = 0; $i < count($info); $i++)
  {
    echo "$first<strong>{$info[$i]["name"]}</strong><br/>";
    $first = "<br/>";
    if ($info[$i]["required"] != -1)
    {
      for ($x = 0; $x <= 5; $x++)
      {
        if ($x != 5) echo $info[$i]["required"][$x]." ";
        else echo pdate($info[$i]["required"][$x]);
        if ($x == 3) echo "<br/>";
      }
      echo "<br/>";
    }
    if (!empty($info[$i]["error"]))
    {
      $prod = travian_extract_production($txt, True);
      $res = travian_extract_resource_count($txt, True);
      $m = 0;
      unset($missing, $time);
      for ($x = 0; $x < 4; $x++)
      {
        $missing[] = $info[$i]["required"][$x] - $res[$x][0];
        $time[] = round((($missing[$x] / $prod[$x]) * 3600));
        if ($prod[$x] < 0) $time[$x] = -1;
        if ($missing[$x] < 0)
        {
          $m++;
          $missing[$x] = 0;
        }
      }
      if ($m == 4) echo $info[$i]["error"];
      else
      {
        print_link("main.php?mod=chgvill&gid=17&submod=send&trade=trade&wood={$missing[0]}".
                   "&clay={$missing[1]}&iron={$missing[2]}&crop={$missing[3]}".
                   "&vname={$_SESSION["village"]}&return=details_build".ps(), "Resources missing:");
        $res = array("Wood", "Clay", "Iron", "Crop");
        for ($x = 0; $x < 4; $x++)
        {
          if ($missing[$x] > 0) echo "<br/>".$res[$x].": ".$missing[$x]." (".pdate($time[$x], False).")";
        }
      }
    }
    else
    {
      print_link("main.php?mod=direct_call&amp;file=$file&amp;".htmlspecialchars($info[$i]["link"]["params"])."&amp;return=$return".ps(), "Build");
    }
    echo "<br/>";
  }
  if (count($info) == 1) echo print_link("main.php?mod=details_build&amp;id={$req["id"]}".ps(), "Details", "<br/>");
  echo "<br/>";